package com.threaddemo1;

public class WatiNotifyDemo {

    public static void main(String[] args) {
        //对象锁:任何对象都是可以的
        //class锁:class对象
        Object lock = new Object();

        //一个线程去获取锁 获取锁之后wait阻塞 释放锁
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("进入线程A");
                synchronized (lock){
                    System.out.println("进入线程A A拿到锁");
                    /*try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
                    try {
                        lock.wait();//执行wait 会释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程A沉睡结束");
                }
                System.out.println("线程A执行结束 A线程释放锁");
            }
        }).start();
        //另一个线程去获取锁 执行通知 notify
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("进入线程B");
                synchronized (lock){
                    System.out.println("进入线程B  B拿到锁");
                    lock.notify();
                }
                System.out.println("线程B执行结束");
            }
        }).start();
    }

}
